/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.acmwustl.bubble.logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author officer
 */
public class ConfigManager {
    private static ConfigManager instance;

    private String pricesConfigFileLoc = "config/prices.cfg";
    private File pricesConfigFile;

    public static ConfigManager getInstance() {
        if(instance == null)
            instance = new ConfigManager();
        return instance;
    }

    private ConfigManager() {
        pricesConfigFile = new File(pricesConfigFileLoc);
        if(!pricesConfigFile.exists()) {
            JOptionPane.showMessageDialog(null,
                    "Error: Config file does not exist.\n"+pricesConfigFileLoc,
                    "No Config File", JOptionPane.ERROR_MESSAGE);
        }
        //TODO: the rest of this class
    }

    private void addNewItemToDocument(String itemName, int costInCents) {
        String toAdd = itemName + "," + costInCents + "\n";
        try {
            FileOutputStream fos = new FileOutputStream(pricesConfigFile, true);
            fos.write(toAdd.getBytes());
            fos.close();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Error while writing to config file.",
                    "I/O Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void updateItems(ArrayList<Item> items) {
        try {
            pricesConfigFile.delete();
            pricesConfigFile.createNewFile();
            for (Item i : items) {
                addNewItemToDocument(i.getName(), i.getPriceInCents());
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null,
                    "Error while updating config file.\n" +
                    "Expect corrupted config file.",
                    "I/O Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public ArrayList<Item> getWares() {
        try {
            ArrayList<Item> items = new ArrayList<Item>();
            Scanner sn = new Scanner(pricesConfigFile);
            while(sn.hasNextLine()) {
                String entry = sn.nextLine();
                StringTokenizer st = new StringTokenizer(entry, ",");
                if(st.countTokens() > 2) {
                    JOptionPane.showMessageDialog(null, "Config file corrupt!",
                            "I/O Error", JOptionPane.ERROR_MESSAGE);
                    break;
                } else {
                    items.add(new Item(st.nextToken(), Integer.valueOf(st.nextToken())));
                }
            }
            sn.close();
            return items;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
